package com.jiamao.goplay.ui.user;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.databinding.ObservableField;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.AnonymousReportOtherLayoutBinding;
import com.jiamao.goplay.ui.user.vm.AnonymousReportOtherVM;
import com.jiamao.goplay.util.KeyInputUtils;
import com.jiamao.goplay.view.HorizontalItemDecoration;
import com.jiamao.goplay.view.WrapHeightGridLayoutManager;
import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.config.ISListConfig;

import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 匿名举报其他理由页
 */
public class AnonymousReportOtherFragment  extends BaseMVVMFragment<AnonymousReportOtherLayoutBinding, AnonymousReportOtherVM> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.anonymous_report_other_layout;
    }

    @Override
    public AnonymousReportOtherVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(AnonymousReportOtherVM.class);
    }
    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.showPhoto.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer aBoolean) {

                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                showPhoto();
            }
        });
    }
    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.uid = getArguments().getString("uid");
        WrapHeightGridLayoutManager gm = new WrapHeightGridLayoutManager(getContext(), 3);
        binding.photoListView.setLayoutManager(gm);
        binding.photoListView.addItemDecoration(new HorizontalItemDecoration(8,getContext()));
        gm.setAutoMeasureEnabled(true);

        TextView textViewTitle = binding.getRoot().findViewById(R.id.tvTitleReport);
        textViewTitle.setText("匿名举报");
        binding.getRoot().findViewById(R.id.ll_back_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });

        //提交举报
        binding.getRoot().findViewById(R.id.ll_submit_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (binding.inputContent.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请输入举报理由");
                } else {
                    ToastUtils.showShort(binding.inputContent.getText().toString());

                    viewModel.sendPost();
                }
            }
        });

        //输入字数统计
        binding.inputContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                binding.maxLen.setText(String.format("%s/150", editable.length()));
            }
        });

        new Handler().postDelayed(new Runnable(){
            public void run() {
                binding.inputContent.requestFocus();
                KeyInputUtils.showInputMethodWindow(binding.inputContent);
            }
        }, 500);
    }


    public void showPhoto(){
        // 自由配置选项
        ISListConfig config = new ISListConfig.Builder()
                // 是否多选, 默认true
                .multiSelect(false)
                // 是否记住上次选中记录, 仅当multiSelect为true的时候配置，默认为true
                .rememberSelected(false)
                // “确定”按钮背景色
                .btnBgColor(Color.GRAY)
                // “确定”按钮文字颜色
                .btnTextColor(Color.BLUE)
                // 使用沉浸式状态栏
                .statusBarColor(Color.WHITE)
                // 返回图标ResId
                .backResId(R.mipmap.ic_black_back)
                // 标题
                .title("图片")
                // 标题文字颜色
                .titleColor(Color.BLACK)
                // TitleBar背景色
                .titleBgColor(Color.WHITE)
                // 裁剪大小。needCrop为true的时候配置
                .cropSize(1, 1, 200, 200)
                .needCrop(true)
                // 第一个是否显示相机，默认true
                .needCamera(false)
                // 最大选择图片数量，默认9
                .maxNum(1)
                .build();

// 跳转到图片选择器
        ISNav.getInstance().toListActivity(this, config, 1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片选择结果回调
        if (requestCode ==  1  && data != null) {
            List<String> pathList = data.getStringArrayListExtra("result");

            viewModel.uploadImage(pathList.get(0),0);

        }
    }


}
